package com.sd.repay.controller;

import com.sd.repay.annotation.ClientVersionCheck;
import com.sd.repay.annotation.MerAuthStatusCheck;
import com.sd.repay.cache.BankCache;
import com.sd.repay.cache.SysDict;
import com.sd.repay.common.CommonResult;
import com.sd.repay.common.Constants;
import com.sd.repay.common.Page;
import com.sd.repay.pojo.*;
import com.sd.repay.realm.CaptchaException;
import com.sd.repay.realm.LoginToken;
import com.sd.repay.service.*;
import com.sd.repay.utils.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping(value = "/client")
public class ClientController {
    private static final Logger log = LoggerFactory.getLogger(ClientController.class);

    @Autowired
    private ClientVersionService clientVersionService;
    @Autowired
    private CardManageService cardManageService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private RepayPlanDetailService repayPlanDetailService;
    @Autowired
    private RepayPlanService repayPlanService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private MerFeeService merFeeService;
    @Autowired
    private RankService rankService;


    @RequestMapping(value = "/needLogin")
    public String needLogin(HttpServletRequest request, HttpServletResponse response) {
        CommonResult res = CommonResult.build(403, "请登录后重试");
        if (WebUtil.isAjaxRequest(request)) {
            return JsonUtil.objectToJson(res);
        }
        WebUtil.writeJson(res, response);
        return null;
    }


    /**
     * 获取图形验证码，返回图片流
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getCaptchaCodeImage")
    public void getCaptchaCodeImage(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");

        // 不缓存此内容
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);

        try {
            HttpSession session = request.getSession();
            CaptchaUtil tool = new CaptchaUtil();
            StringBuffer code = new StringBuffer();
            BufferedImage image = tool.genRandomCodeImage(code);
            session.removeAttribute(Constants.CLIENT_CAPTCHA_KEY);
            session.setAttribute(Constants.CLIENT_CAPTCHA_KEY, code.toString());
            // 将内存中的图片通过流动形式输出到客户端
            ImageIO.write(image, "JPEG", response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取图形验证码字符串(Base64)
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getCaptchaCode")
    @ResponseBody
    public CommonResult getCaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        CaptchaUtil tool = new CaptchaUtil();
        StringBuffer code = new StringBuffer();
        BufferedImage image = tool.genRandomCodeImage(code);
        session.removeAttribute(Constants.CLIENT_CAPTCHA_KEY);
        session.setAttribute(Constants.CLIENT_CAPTCHA_KEY, code.toString());
        String codeBase64 = new String(Base64.encodeBase64(code.toString().getBytes("UTf-8")), "UTF-8");
        return CommonResult.ok(codeBase64);
    }


    /**
     * 发送短信验证码
     *
     * @param request
     * @param params
     * @throws Exception
     */
    @RequestMapping(value = "/getSmsCode", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult getSmsCode(HttpServletRequest request, @RequestParam Map<String, String> params) throws Exception {
        String busiType = params.get("busiType");
        String mobileNo = params.get("mobileNo");
        if (StringUtil.isBlank(busiType)) {
            return CommonResult.build(403, "必要的参数不能为空");
        }
        String dictValue = SysDict.getDictValue("SMS_BUSINESS", busiType);
        if (StringUtil.isBlank(dictValue)) {
            return CommonResult.build(403, "业务类型异常");
        }

        SmsInfo smsInfo = new SmsInfo();
        smsInfo.setMobiles(mobileNo);
        smsInfo.setSource_system(Constants.SYSTEM_SOURCE_CLIENT);
        smsInfo.setSms_type("1");
        smsInfo.setSms_business(busiType);
        smsService.smsSend(smsInfo);
        return CommonResult.build(200, "短信已发送");
    }

    /**
     * 检查客户端版本
     *
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/checkClinetVersion", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult checkClinetVersion(HttpServletRequest request, @RequestParam Map<String, String> params) {
        log.info("检查客户端版本更新");

        String osType = params.get("osType");
        String osVersion = params.get("osVersion");
        String clientVersion = params.get("clientVersion");
        if (StringUtil.isBlank(osType, osVersion, clientVersion)) {
            return CommonResult.build(403, "必要参数不能有空值");
        }

        ClientVersion cv = new ClientVersion();
        cv.setOsType(osType);
        cv.setDownFlag(Constants.CLIENT_VERSION_DOWN_UN);

        try {
            Map<String, Object> clientVersionMap = clientVersionService.getClientVersion(osType, clientVersion);
            if (clientVersionMap == null) {
                return CommonResult.build(403, "客户端版本异常");
            }
            String isNew = (String) clientVersionMap.get("is_new");
            if (!Constants.CLIENT_VERSION_IS_NEW.equals(isNew)) {
                Long currIndex = (Long) clientVersionMap.get("id");
                Map<String, Object> newClientVersionMap = clientVersionService.getMustClientVersion(osType, currIndex, Constants.CLIENT_VERSION_DOWN_MUST);
                if (newClientVersionMap == null) {
                    newClientVersionMap = clientVersionService.getMustClientVersion(osType, currIndex, Constants.CLIENT_VERSION_DOWN_OR);
                }

                if (newClientVersionMap != null) {
                    cv.setDownFlag((String) newClientVersionMap.get("down_flag"));
                    cv.setClientVersion((String) newClientVersionMap.get("client_version"));
                    cv.setDesc((String) newClientVersionMap.get("desc"));
                    cv.setClientUrl((String) newClientVersionMap.get("client_url"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("检查客户端版本更新异常：" + e);
            return CommonResult.build(403, "检查客户端版本更新异常");
        }

        return new CommonResult(200, "成功", cv);
    }


    /**
     * 登录
     *
     * @param request
     * @param params
     * @return
     */
    @ClientVersionCheck
    @RequestMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult login(HttpServletRequest request, @RequestParam Map<String, String> params) {

        String userName = params.get("userName");
        String loginPwd = params.get("loginPwd");
        String oemType = params.get("oemType");
        String captcha = params.get("captcha");
        log.info("----登录：{}", userName);

        if (StringUtil.isBlank(userName, loginPwd)) {
            return CommonResult.build(403, "必要参数不能有空值");
        }
        if (StringUtil.isBlank(oemType)) {
            oemType = Constants.DEFAULT_OEM_TYPE;
        }

        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new LoginToken(userName, loginPwd, oemType, captcha));
        } catch (CaptchaException ex) {
            log.error("验证码错误");
            return CommonResult.build(403, "验证码错误");
        } catch (LockedAccountException ex) {
            log.error("用户状态异常");
            return CommonResult.build(403, "用户状态异常");
        } catch (UnknownAccountException ex) {
            log.error("用户名不存在");
            return CommonResult.build(403, "用户名不存在");
        } catch (AuthenticationException ex) {
            log.error("用户名或密码错误");
            return CommonResult.build(403, "用户名或密码错误");
        } catch (Exception e) {
            log.error("登录失败");
            return CommonResult.build(403, "登录失败");
        }

        CommonResult result = new CommonResult();
        boolean loginFlag = subject.isAuthenticated();
        if (loginFlag) {
            result.setStatus(200);
            result.setMsg("登录成功");
            result.setData(SecurityUtils.getSubject().getPrincipal());

            //获取登录成功后返回的数据
        } else {
            result.setStatus(403);
            result.setMsg("登录失败");
        }
        return result;
    }


    /**
     * 修改登录密码
     *
     * @param request
     * @param response
     * @param params
     * @throws Exception
     */
    @ClientVersionCheck
    @RequestMapping(value = "/updateLoginPwd", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult updateLoginPwd(HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, String> params) throws Exception {
        String loginPwd = params.get("loginPwd");
        String newLoginPwd = params.get("newLoginPwd");
        String confirmLoginPwd = params.get("confirmLoginPwd");
        if (StringUtil.isBlank(loginPwd, newLoginPwd, confirmLoginPwd)) {
            return CommonResult.build(403, "必要的参数不能为空");
        }
        if (!newLoginPwd.trim().equals(confirmLoginPwd.trim())) {
            return CommonResult.build(403, "两次输入新密码不一致");
        }

        MerchantUser merchantUser = (MerchantUser) SecurityUtils.getSubject().getPrincipal();
        String loginPwdDb = Md5Util.MD5Encode(loginPwd, "UTF-8", false);

        MerchantUser verifyMerchantUser = merchantService.getMerObjectByMerchantNoAndPwd(merchantUser.getOemType(), merchantUser.getMerchantNo(), loginPwdDb);
        if (verifyMerchantUser == null) {
            return CommonResult.build(403, "登录密码错误");
        }
        String newLoginPwdDb = Md5Util.MD5Encode(newLoginPwd, "UTF-8", false);
        int updateRet = merchantService.updateMerchantLoginPwd(merchantUser.getOemType(), merchantUser.getMerchantNo(), newLoginPwdDb);
        if (updateRet != 1) {
            return CommonResult.build(403, "交易失败");
        }

        return CommonResult.ok();
    }

    /**
     * 找回登录密码
     *
     * @param request
     * @param params
     * @throws Exception
     */
    @ClientVersionCheck
    @RequestMapping(value = "/findLoginPwd", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult findLoginPwd(HttpServletRequest request, @RequestParam Map<String, String> params) throws Exception {
        String smsCode = params.get("smsCode");
        String mobileNo = params.get("mobileNo");
        String oemType = params.get("oemType");
        String newLoginPwd = params.get("newLoginPwd");
        String newLoginPwd2 = params.get("newLoginPwd2");


        if (StringUtil.isBlank(smsCode, mobileNo, newLoginPwd, newLoginPwd2)) {
            return CommonResult.build(403, "必要的参数不能为空");
        }

        if (!newLoginPwd.trim().equals(newLoginPwd2.trim())) {
            return CommonResult.build(403, "两次输入新密码不一致");
        }

        if (StringUtil.isBlank(oemType)) {
            oemType = Constants.DEFAULT_OEM_TYPE;
        }

        Map<String, Object> merInfoMap = merchantService.getMerInfoByMobileAndOem(mobileNo, oemType);

        if (merInfoMap == null || merInfoMap.isEmpty()) {
            return CommonResult.build(403, "手机号未注册");
        }

        String merchantNo = merInfoMap.get("merchant_no").toString();
        String merMobileNo = merInfoMap.get("mobile_no").toString();

        //验证短信验证码
        SmsInfo smsInfo = new SmsInfo();
        smsInfo.setMobiles(merMobileNo);
        smsInfo.setSms_business(Constants.SMS_BUSINESS_FIND_LOGINPWD);
        smsInfo.setVali_code(smsCode);
        smsService.checkValidateCode(smsInfo);
        if (!"2".equals(smsInfo.getVali_status())) {
            return CommonResult.build(403, "短信验证码验证输入有误");
        }

        String newLoginPwdDb = Md5Util.MD5Encode(newLoginPwd, "UTF-8", false);
        int updateRet = merchantService.updateMerchantLoginPwd(oemType, merchantNo, newLoginPwdDb);
        if (updateRet != 1) {
            return CommonResult.build(403, "交易失败");
        }
        return CommonResult.ok();
    }

    /**
     * 获取商户信息
     *
     * @param request
     * @param params
     * @return
     */
    @ClientVersionCheck
    @RequestMapping(value = "/merchantInfo", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult merchantInfo(HttpServletRequest request, @RequestParam Map<String, String> params) {
        MerchantUser merchantUser = (MerchantUser) SecurityUtils.getSubject().getPrincipal();
        String idCardNo = merchantUser.getIdCardNo();

        if ("1".equals(merchantUser.getIsAuth()) && !StringUtil.isBlank(idCardNo)) {
            try {
                Map<String, Object> cardMap = cardManageService.getSettleCardInfoByIdCard(idCardNo);
                if (cardMap != null) {
                    String cardSettleBankNo = (String) cardMap.get("settle_bank_no");
                    String cardSettleBankName = BankCache.getBankName("local", cardSettleBankNo);

                    merchantUser.setSettleCardNo((String) cardMap.get("card_no"));
                    merchantUser.setSettleAccountNo((String) cardMap.get("account_no"));
                    merchantUser.setCardSettleBankNo(cardSettleBankNo);
                    merchantUser.setCardSettleBankName(cardSettleBankName);
                    merchantUser.setCardBankNo((String) cardMap.get("bank_no"));
                    merchantUser.setCardBankName((String) cardMap.get("bank_name"));
                    merchantUser.setCardProvince((String) cardMap.get("province"));
                    merchantUser.setCardCity((String) cardMap.get("city"));
                }

                //获取商户费率信息
                Map<String, Object> merFeeInfo = merFeeService.getMerFeeByMerNoAndOemType(Constants.MER_TYPE_MERCHANT, merchantUser.getMerchantNo(), merchantUser.getOemType());
                merchantUser.setMerFeeInfo(merFeeInfo);

                //封装分享注册链接
                String repayAccessUrl = WebUtil.getSysValue("REPAY_ACCESS_URL");
                repayAccessUrl = StringUtils.isBlank(repayAccessUrl) ? "http://192.168.4.33:5050/" : repayAccessUrl;
                String shareRegUrl = repayAccessUrl + "shareMgr/shareRegUrl?parentMerNo=" + merchantUser.getMerchantNo() + "&oemType=" + merchantUser.getOemType();
                log.info("商户号[{}]分享注册链接[{}]", new Object[]{merchantUser.getMerchantNo(), shareRegUrl});
                shareRegUrl = Base64Utils.encodeToString(shareRegUrl.getBytes("UTF-8"));
                merchantUser.setShareRegUrl(shareRegUrl);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        CommonResult result = new CommonResult();
        result.setStatus(200);
        result.setMsg("交易成功");
        result.setData(merchantUser);

        return result;
    }

    /**
     * 卡管理
     *
     * @param request
     * @param params
     * @throws Exception
     */
    @ClientVersionCheck
    @MerAuthStatusCheck
    @RequestMapping(value = "/cardManage", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult cardManage(HttpServletRequest request, @RequestParam Map<String, String> params) {
        MerchantUser merchantUser = (MerchantUser) SecurityUtils.getSubject().getPrincipal();
        String idCardNo = merchantUser.getIdCardNo();

        List<Map<String, Object>> cardList = new ArrayList<>();
        try {
            cardList = cardManageService.getCardInfoByIdCard(idCardNo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (Map<String, Object> map : cardList) {
            String planStatus = Constants.CARD_NO_PLAN;
            String cardNo = (String) map.get("card_no");
            String batchNo = "";
            try {
                List<RepayPlan> tempList = repayPlanService.getInExecutePlanByCardNo(cardNo);
                if (tempList != null && tempList.size() > 0) {
                    planStatus = Constants.CARD_IS_PLAN;
                    batchNo = tempList.get(0).getBatchNo();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            map.put("planStatus", planStatus);
            map.put("batchNo", batchNo);
        }

        CommonResult result = new CommonResult();
        result.setStatus(200);
        result.setMsg("交易成功");
        result.setData(cardList);
        return result;
    }


    /**
     * 我的商户列表(直属的二级商户)
     *
     * @param request
     * @param params
     * @throws Exception
     */
    @ClientVersionCheck
    @RequestMapping(value = "/myMerList", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult myMerList(HttpServletRequest request, @RequestParam Map<String, String> params) throws Exception {
        MerchantUser merchantUser = (MerchantUser) SecurityUtils.getSubject().getPrincipal();
        String merchantNo = merchantUser.getMerchantNo();
        String oemType = merchantUser.getOemType();

        List<Map<String, Object>> merList = null;
        try {
            merList = merchantService.getSubMerchant(oemType, merchantNo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        CommonResult result = new CommonResult();
        result.setStatus(200);
        result.setMsg("交易成功");
        result.setData(merList);
        return result;
    }

    /**
     * 计划查询
     *
     * @param request
     * @param params
     * @throws Exception
     */
    @ClientVersionCheck
    @MerAuthStatusCheck
    @RequestMapping(value = "/planlist", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult planlist(HttpServletRequest request, @RequestParam Map<String, String> params) throws Exception {
        CommonResult result = new CommonResult();
        result.setStatus(200);
        result.setMsg("交易成功");

        String startTime = params.get("startTime");
        String endTime = params.get("endTime");
        String pageNo = params.get("pageNo");
        String pageSize = params.get("pageSize");
        String status = StringUtil.ifEmptyThen(params.get("status"), "");

        if (StringUtil.isBlank(startTime, endTime, pageNo, pageSize)) {
            result.setStatus(403);
            result.setMsg("必要的参数不能为空");
            log.error(result.getStatus() + "|" + result.getMsg());
            return result;
        }

        MerchantUser merchantUser = (MerchantUser) SecurityUtils.getSubject().getPrincipal();
        String merchantNo = merchantUser.getMerchantNo();
        Page<Map<String, Object>> page = null;
        try {
            page = repayPlanService.getRepayPlanPage(merchantNo, status, startTime, endTime, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        }

        result.setData(page.getData());
        return result;
    }

    /**
     * 计划详情查询
     *
     * @param request
     * @param params
     * @throws Exception
     */
    @ClientVersionCheck
    @MerAuthStatusCheck
    @RequestMapping(value = "/planDetail", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult planDetail(HttpServletRequest request, @RequestParam Map<String, String> params) throws Exception {
        CommonResult result = new CommonResult();
        String planNo = params.get("batchNo");
        String status = StringUtil.ifEmptyThen(params.get("status"), "");
        MerchantUser merchantUser = (MerchantUser) SecurityUtils.getSubject().getPrincipal();

        List<RepayPlanDetail> list = new ArrayList<>();
        BigDecimal repayAmount = BigDecimal.ZERO;
        try {
            RepayPlan repayPlan = repayPlanService.getRepayPlanByBatchNo(planNo);
            repayAmount = repayPlan.getRepayAmount();
            if (repayPlan == null || !merchantUser.getMerchantNo().equals(repayPlan.getMerchantNo())) {
                result.setStatus(403);
                result.setMsg("计划不存在或商户异常");
                log.error("查询异常：" + result.getMsg());
                return result;
            }
            if ("".equals(status)) {
                list = repayPlanDetailService.getDetailByBatchNo(planNo);
            } else {
                list = repayPlanDetailService.getDetailByBatchNoAndStatus(planNo, status);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        BigDecimal amountDone = BigDecimal.ZERO;
        BigDecimal amountConsume = BigDecimal.ZERO;
        List<Map<String, String>> repayPlanDetailList = new ArrayList<>();

        for (RepayPlanDetail detail : list) {
            BigDecimal amount = detail.getPlanAmount();
            String planType = detail.getPlanType();
            String planStauts = detail.getPlanStatus();
            Date time = detail.getPlanTime();
            int planIndex = detail.getPlanIndex();

            Map<String, String> map = new HashMap<>();
            map.put("orderNo", detail.getPlanNo());
            map.put("amount", amount.toPlainString());
            map.put("planType", planType);
            map.put("planStauts", planStauts);
            map.put("planTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
            map.put("planIndex", String.valueOf(planIndex));

            repayPlanDetailList.add(map);

            if (Constants.PLAN_DETAIL_STATUS_SUCCESS.equals(planStauts)) {
                if (Constants.PLAN_DETAIL_TYPE_IN.equals(planType)) {
                    amountDone = amountDone.add(amount);
                } else {
                    amountConsume = amountConsume.add(amount);
                }
            }
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("amountUnDo", repayAmount.subtract(amountDone).toPlainString());
        resultMap.put("amountDone", amountDone.toPlainString());
        resultMap.put("amountConsume", amountConsume.toPlainString());
        resultMap.put("tradeList", repayPlanDetailList);

        result.setStatus(200);
        result.setMsg("交易成功");
        result.setData(resultMap);
        return result;
    }

    /**
     * 获取等级信息
     *
     * @param request
     * @param params
     * @throws Exception
     */
    @ClientVersionCheck
    @RequestMapping(value = "/rankConfigInfo", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult rankConfigInfo(HttpServletRequest request, @RequestParam Map<String, String> params) throws Exception {
        try {
            String oemType = params.get("oemType");
            oemType = StringUtils.isBlank(oemType) ? Constants.DEFAULT_OEM_TYPE : oemType;

            Map<String, Map<String, Object>> rankInfoRes = new LinkedHashMap<>();
            rankInfoRes.put(Constants.RANK_CODE_SILVER, rankService.getRankConfigByOemAndCode(oemType, Constants.RANK_CODE_SILVER));
            rankInfoRes.put(Constants.RANK_CODE_GOLD, rankService.getRankConfigByOemAndCode(oemType, Constants.RANK_CODE_GOLD));
            rankInfoRes.put(Constants.RANK_CODE_PLATINUM, rankService.getRankConfigByOemAndCode(oemType, Constants.RANK_CODE_PLATINUM));
            rankInfoRes.put(Constants.RANK_CODE_DIAMONDS, rankService.getRankConfigByOemAndCode(oemType, Constants.RANK_CODE_DIAMONDS));

            return CommonResult.ok(rankInfoRes);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取等级信息异常[{}]", e);
            return CommonResult.build(403, "操作失败，请稍候再试");
        }
    }

    /**
     * 退出登录
     *
     * @throws Exception
     */
    @RequestMapping(value = "/logout", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult logout(HttpServletRequest request) {
        try {
            SecurityUtils.getSubject().logout();
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("退出登录异常[{}]", e);
            return CommonResult.build(403, "操作失败，请稍候再试");
        }
    }
}
